package com.findviewbydream.evil.eda.Activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.findviewbydream.evil.eda.Bean.OverAnswerBean;
import com.findviewbydream.evil.eda.Data.Data;
import com.findviewbydream.evil.eda.Data.UserData;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Dialog.LoadingDialog;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuyh.easyadapter.recyclerview.EasyRVAdapter;
import com.yuyh.easyadapter.recyclerview.EasyRVHolder;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.iwgang.familiarrecyclerview.FamiliarRecyclerView;
import okhttp3.Call;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Activity
 * 时间：16-9-14 下午4:55
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：已经答题完成的列表Activity
 ********************************************/
public class OverAnswerActivity extends BaseActivity
{
    private static final String QRURL = "http://qr.topscan.com/api.php?text=http://183.196.110.7:8888/EdaSupport/app";
    private Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private AppBarLayout appBarLayout;
    private FamiliarRecyclerView recyclerView;
    private CoordinatorLayout coordinatorLayout;
    private ImageView pull_down_background;
    private TextView userPhoneNum;
    private TextView user_over;
    private TextView user_yes;
    private TextView user_flow;
    private ImageView qr_img;

    //private TextView user_lastdate;

    private Adapter adapter;
    private ArrayList<OverAnswerBean> list;
    private Map<String, String> userInfo;


    @Override
    public int setContentView()
    {
        return R.layout.over_answer_activity;
    }

    @Override
    public String setToolbarTitle()
    {
        return "";
    }

    @Override
    public void onCreate()
    {
        initView();
        setRecyclerView();
        setCollapsingToolbarLayout();

        LoadingDialog.showDialog(this);
        toNetWork();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        recyclerView = (FamiliarRecyclerView) findViewById(R.id.recyclerView);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        pull_down_background = (ImageView) findViewById(R.id.pull_down_background);
        userPhoneNum = (TextView) findViewById(R.id.userPhoneNum);
        user_over = (TextView) findViewById(R.id.user_over);
        user_flow = (TextView) findViewById(R.id.user_flow);
        //user_lastdate = (TextView) findViewById(R.id.user_lastdate);
        user_yes = (TextView) findViewById(R.id.user_yes);
        qr_img = (ImageView) findViewById(R.id.doorSign);
    }

    /**
     * 设置下划toolbar
     */
    private void setCollapsingToolbarLayout()
    {
        Utils.Glide(R.drawable.over_answer_background, pull_down_background);

        collapsingToolbarLayout.setTitle("已答列表");
        collapsingToolbarLayout.setExpandedTitleColor(Color.TRANSPARENT);//设置还没收缩时状态下字体颜色
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);//设置收缩后Toolbar上字体的颜色
    }

    /**
     * 网络连接
     */
    private void toNetWork()
    {
        OkHttpUtils.post().url(Data.IP + "OverAnswerServlet")
                .addParams("phone", UserData.getUserPhoneNum())
                .addParams("imei", UserData.getUserIMEI())
                .build()
                .execute(new StringCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        error();
                    }

                    @Override
                    public void onResponse(String response, int id)
                    {
                        String[] ss = response.split(Data.BREAKSTRING);

                        if (!ss[0].isEmpty())
                        {
                            userInfo = new Gson().fromJson(ss[0], new TypeToken<Map<String, String>>()
                            {
                            }.getType());

                            if (!ss[1].isEmpty())
                            {
                                list = new Gson().fromJson(ss[1], new TypeToken<ArrayList<OverAnswerBean>>()
                                {
                                }.getType());

                                setTopInfo();/*设置顶部信息*/
                                LoadingDialog.dismissDialog();

                                if (list == null || list.size() == 0)
                                    findViewById(R.id.null_sign).setVisibility(View.VISIBLE);
                                else
                                    setRecyclerView();/*设置列表信息*/
                            }
                            else
                                error();
                        }
                        else
                            error();
                    }

                    /**
                     * 用于加载失败
                     */
                    private void error()
                    {
                        Utils.Toast("加载失败！");
                        LoadingDialog.showError(OverAnswerActivity.this, () -> toNetWork());
                    }
                });
    }

    /**
     * 设置顶部信息
     */
    @SuppressLint("SetTextI18n")
    private void setTopInfo()
    {
        userPhoneNum.setText("用户：" + UserData.getUserPhoneNum());
        user_over.setText("已经完成" + userInfo.get("SUM(OVER)") + "道题目");
        user_yes.setText("共计答对" + (Integer.valueOf(userInfo.get("SUM(OVER)")) - Integer.valueOf(userInfo.get("SUM(ERROR)"))) + "道题目");
        user_flow.setText("已经获得" + userInfo.get("SUM(FLOW)") + "M流量");
        //user_lastdate.setText("您上次答题在" + userInfo.get("LAST_DATATIME"));

        Utils.Glide(QRURL, qr_img);

        qr_img.setOnClickListener(view -> {
            Utils.Logger("二维码点击");
            startActivity(new Intent(this, ImageZoomActivity.class)
                    .putExtra(ImageZoomActivity.IMGAPI
                            , QRURL));
        });

    }

    /**
     * 设置RecyclerView
     */
    private void setRecyclerView()
    {
        adapter = new Adapter(this, list, R.layout.over_answer_item);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 适配器
     */
    private class Adapter extends EasyRVAdapter<OverAnswerBean>
    {
        public Adapter(Context context, List<OverAnswerBean> list, int... layoutIds)
        {
            super(context, list, layoutIds);
        }

        @Override
        protected void onBindData(EasyRVHolder viewHolder, int position, OverAnswerBean item)
        {
            ImageView item_img = viewHolder.getView(R.id.item_img);
            TextView item_title = viewHolder.getView(R.id.item_title);
            TextView item_userPhone = viewHolder.getView(R.id.item_userPhone);
            TextView item_yes_no = viewHolder.getView(R.id.item_yes_no);
            TextView item_flow = viewHolder.getView(R.id.item_flow);
            TextView item_datetime = viewHolder.getView(R.id.item_datetime);
            ImageView item_yes_no_img = viewHolder.getView(R.id.item_yes_no_img);

            if (!item.getImgPath().isEmpty())
                Utils.Glide(Utils.getNetImg(item.getImgPath()), item_img);

            item_title.setTypeface(Typeface.DEFAULT_BOLD);
            item_title.setText(item.getTitle());
            item_userPhone.setText(item.getPhone());
            item_yes_no.setText("正确：" + item.getYesNum() + "道题目   |    错误：" + item.getNoNum() + "道题目");
            item_flow.setText("获得流量：" + item.getSumFlow() + "M");
            item_datetime.setText("完成时间：" + item.getOvertime());

            if (item.getYesNum() == 0)
                Utils.Glide(R.drawable.wrong, item_yes_no_img);
            else
                Utils.Glide(R.drawable.correct, item_yes_no_img);
        }
    }


}
